/*******************************************************************************
 * Copyright (c) 2004, 2010 Elias Volanakis and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Elias Volanakis - initial API and implementation
 *******************************************************************************/
package org.asem.eclipse.mii.model.editor;

import org.asem.eclipse.mii.model.ShapesPlugin;
import org.asem.eclipse.mii.model.abs.ShapeConstants;
import org.asem.eclipse.mii.model.abs.ShapesFactory;
import org.asem.eclipse.mii.model.shapes.ControllerShape;
import org.asem.eclipse.mii.model.shapes.ModelShape;
import org.asem.eclipse.mii.model.shapes.StoreShape;
import org.asem.eclipse.mii.model.shapes.ViewShape;
import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteToolbar;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.jface.resource.ImageDescriptor;


/**
 * Utility class that can create a GEF Palette.
 * 
 * @see #createPalette()
 * @author Elias Volanakis
 */
final class ShapesEditorPaletteFactory {

    /** Create the "Shapes" drawer. */
    private static PaletteContainer createShapesDrawer() {
        PaletteDrawer componentsDrawer = new PaletteDrawer("Shapes");

        CombinedTemplateCreationEntry component = new CombinedTemplateCreationEntry("Model",
                "Create an model shape", ModelShape.class, ShapesFactory.getInstance(ModelShape.class),
                ImageDescriptor.createFromFile(ShapesPlugin.class, ModelShape.MODEL_ICON_PATH),
                ImageDescriptor.createFromFile(ShapesPlugin.class, ModelShape.MODEL_ICON_PATH));
        componentsDrawer.add(component);
        
        component = new CombinedTemplateCreationEntry("Store",
                "Create an store shape", StoreShape.class, ShapesFactory.getInstance(StoreShape.class),
                ImageDescriptor.createFromFile(ShapesPlugin.class, StoreShape.MODEL_ICON_PATH),
                ImageDescriptor.createFromFile(ShapesPlugin.class, StoreShape.MODEL_ICON_PATH));
        componentsDrawer.add(component);
        
        component = new CombinedTemplateCreationEntry("Controller",
                "Create an controller shape", ControllerShape.class, ShapesFactory.getInstance(ControllerShape.class),
                ImageDescriptor.createFromFile(ShapesPlugin.class, ControllerShape.MODEL_ICON_PATH),
                ImageDescriptor.createFromFile(ShapesPlugin.class, ControllerShape.MODEL_ICON_PATH));
        componentsDrawer.add(component);
        
        component = new CombinedTemplateCreationEntry("View",
                "Create an view shape", ViewShape.class, ShapesFactory.getInstance(ViewShape.class),
                ImageDescriptor.createFromFile(ShapesPlugin.class, ViewShape.MODEL_ICON_PATH),
                ImageDescriptor.createFromFile(ShapesPlugin.class, ViewShape.MODEL_ICON_PATH));
        componentsDrawer.add(component);

        return componentsDrawer;
    }

    /**
     * Creates the PaletteRoot and adds all palette elements. Use this factory method to create a new palette for your
     * graphical editor.
     * 
     * @return a new PaletteRoot
     */
    static PaletteRoot createPalette() {
        PaletteRoot palette = new PaletteRoot();
        palette.add(createToolsGroup(palette));
        palette.add(createShapesDrawer());
        return palette;
    }

    /** Create the "Tools" group. */
    private static PaletteContainer createToolsGroup(PaletteRoot palette) {
        PaletteToolbar toolbar = new PaletteToolbar("Tools");

        // Add a selection tool to the group
        ToolEntry tool = new PanningSelectionToolEntry();
        toolbar.add(tool);
        palette.setDefaultEntry(tool);

        // Add a marquee tool to the group
        toolbar.add(new MarqueeToolEntry());

        // Add (solid-line) connection tool
        tool = new ConnectionCreationToolEntry("Solid connection", "Create a solid-line connection",
                new CreationFactory() {
                    public Object getNewObject() {
                        return null;
                    }

                    // see ShapeEditPart#createEditPolicies()
                    // this is abused to transmit the desired line style
                    public Object getObjectType() {
                        return ShapeConstants.SOLID_CONNECTION;
                    }
                }, 
                ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_s16.gif"),
                ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_s24.gif"));

        toolbar.add(tool);

        // Add (dashed-line) connection tool
        tool = new ConnectionCreationToolEntry("Dashed connection", "Create a dashed-line connection",
                new CreationFactory() {
                    public Object getNewObject() {
                        return null;
                    }

                    // see ShapeEditPart#createEditPolicies()
                    // this is abused to transmit the desired line style
                    public Object getObjectType() {
                        return ShapeConstants.DASHED_CONNECTION;
                    }
                }, 
                ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_d16.gif"),
                ImageDescriptor.createFromFile(ShapesPlugin.class, "icons/connection_d24.gif"));

        toolbar.add(tool);
        return toolbar;
    }

    /** Utility class. */
    private ShapesEditorPaletteFactory() {
        // Utility class
    }

}